package chatController;

import java.net.InetAddress;
import java.net.UnknownHostException;
import common.*;
import chatNI.*;
import chatGUI.*;

/**
 * Mission: controller in the MVC pattern.
 * @author bazile gaye
 */
public class ChatController {

    private ChatNI chatni;
    private ChatGUI chatgui;
    private User user;

    /**
     * Constructor.
     * @throws UnknownHostException
     */
    public ChatController() throws UnknownHostException{
            this.user = new User();
            this.chatgui = null;
            this.chatni = null;
    }

    /**
     * Update of the attribut ChatNI
     * @param cni chatNI
     */
    public void setChatni(ChatNI cni){
            this.chatni = cni;
    }
    /**
     * Update of the attribut ChatGUI
     * @param cgui chatGUI
     */
    public void setChatgui(ChatGUI cgui){
            this.chatgui = cgui;
    }

    /**
     * Get the object user.
     * @return user
     */
    public User getUser(){
            return this.user;
    }



    /**
     * Function uses when the localUser is connected: 
     * it active the ports of chatNI and send by broadcast a Hello message.
     * @param login username of the local user.
     */
    public void connection(String login){ 
            this.user.setUsername(login);
            this.user.changeState(true);
    }

    /**
     * This function change the system's state and clear the nformations about the local user.
     */
    public void deconnection(){
            this.user.changeState(false);
            this.user.removeAll();
            this.user.setUsername("null");
    }

    /**
     * Function used to send text messages.
     * @param mess message to send.
     * @param broad boolean which indicates if the message is a broadcast message or not.
     * @param destUser username of the remote user.
     */

    public void send(String mess, Boolean broad, String destUser){  

        // Création du message
        Text text = new Text(this.user.getIpAddress(), mess);
        // Envoie en broadcast ou en unicast
        if (broad == true){
            this.chatni.sendBroadcast(text);
        } else if (broad == false ){
            InetAddress addest = this.user.getUsersIp(destUser);
            this.chatni.send(addest , text);
        }
    }

    /**
     * Function uses when a message is received by the chatNI.
     * It checks the kind of the received message (Text, bye, hello...) and do things associated to it.
     * @param mess message received
     * @param braod  boolean to know if the message is a broadcast or not.
     */
    public void receiveMessage(Message mess, Boolean braod){

        if (mess instanceof Hello){
            
            Hello mess2 = (Hello) mess;
                       
            if (!mess2.getAddr().equals(this.user.getIpAddress())){
            // mise à jour de la liste des utilisateurs
            this.user.addUser(mess2.getAddr(),mess2.getUsername());

            if (mess2.getReply() == true ){
                    Hello salut = new Hello(this.user.getUsername(), this.user.getIpAddress(), false);
                    chatni.send(mess2.getAddr(), salut);
                    this.chatgui.messageReceived = mess2.getUsername()+" has joined the chat session !";
                    this.chatgui.displayOnOff(mess2.getUsername());
            }
            }

        } else if(mess instanceof Bye) {

             Bye mess2 = (Bye) mess;
             // Mise à jour de la liste des users
             this.chatgui.messageReceived = this.user.getUsers(mess2.getAddr())+" has quit !";            
             this.chatgui.displayOnOff(this.user.getUsers(mess2.getAddr()));
             this.user.remove(mess2.getAddr());

        } else if(mess instanceof TransfertFileRequest) {
             TransfertFileRequest mess2 = (TransfertFileRequest) mess;
             // TODO: Afficher à l'utilisateur la demande de transfert de fichier
        } else if(mess instanceof TransfertFileAccept) {
             TransfertFileAccept mess2 = (TransfertFileAccept) mess;
             // TODO: Afficher à l'utilisateur l'acceptation de transfert de fichier

        } else if(mess instanceof Text) {
            
            Text mess2 = (Text) mess;
            
            if (!mess2.getAddr().equals(this.user.getIpAddress())){
                this.chatgui.messageReceived = mess2.getContent();

                // Gestion du message reçu en broadcast ou non
                if (braod){
                    this.chatgui.afficheBroadcast();
                } else {
                    this.chatgui.afficheMessage(user.getUsers(mess2.getAddr()));
                }
            }

        } else if(mess instanceof Fichier) {
             Fichier mess2 = (Fichier) mess;
             
        }

    }
	
}
